<template>
  <div id="params">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>参数列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-alert
        title="注意：只允许为第三级分类设置相关参数！"
        type="warning"
        effect="dark"
        show-icon
        :closable="false"
      >
      </el-alert>
      <el-row>
        <el-col>
          <span class="fl span">选择商品分类：</span>
          <el-cascader
            expand-trigger="hover"
            v-model="selectedCateKeys"
            :options="cateList"
            :props="cateProps"
            @change="handleChange"
          ></el-cascader>
        </el-col>
      </el-row>
      <el-tabs type="border-card" v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="动态参数" name="many">
          <el-button
            type="primary"
            class="fl"
            size="mini"
            :disabled="isDisabled"
            style="marginBottom:16px"
            @click="addDialogVisible = true"
            >添加参数</el-button
          >
          <el-table :data="manyData" border stripe>
            <el-table-column type="expand">
              <template v-slot="scope">
                <el-tag
                  :key="index"
                  v-for="(item, index) in scope.row.attr_vals"
                  closable
                  :disable-transitions="false"
                  @close="handleClose(index)"
                >
                  {{ item }}
                </el-tag>
                <el-input
                  class="input-new-tag"
                  v-if="scope.row.inputVisible"
                  v-model="scope.row.inputValue"
                  ref="saveTagInput"
                  size="small"
                  @keyup.enter.native="handleInputConfirm(scope.row)"
                  @blur="handleInputConfirm(scope.row)"
                >
                </el-input>
                <el-button
                  v-else
                  class="button-new-tag"
                  size="small"
                  @click="showInput(scope.row)"
                  >+ New Tag</el-button
                >
              </template>
            </el-table-column>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column
              label="参数名称"
              prop="attr_name"
            ></el-table-column>
            <el-table-column label="操作">
              <template v-slot="scope">
                <el-button
                  size="mini"
                  icon="el-icon-edit"
                  type="primary"
                  @click="findId(scope.row.attr_id)"
                  >修改</el-button
                >
                <el-button
                  size="mini"
                  icon="el-icon-delete"
                  type="danger"
                  @click="del(scope.row.attr_id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="静态属性" name="only">
          <el-button
            type="primary"
            class="fl"
            size="mini"
            :disabled="isDisabled"
            style="marginBottom:16px"
            @click="addDialogVisible = true"
            >添加属性</el-button
          >
          <el-table :data="onlyData" border stripe>
            <el-table-column type="expand"> </el-table-column>
            <el-table-column type="index" label="#"></el-table-column>
            <el-table-column
              label="属性名称"
              prop="attr_name"
            ></el-table-column>
            <el-table-column label="操作">
              <template v-slot="scope">
                <el-button
                  size="mini"
                  icon="el-icon-edit"
                  type="primary"
                  @click="findId(scope.row.attr_id)"
                  >修改</el-button
                >
                <el-button
                  size="mini"
                  icon="el-icon-delete"
                  type="danger"
                  @click="del(scope.row.attr_id)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </el-card>
    <!-- 添加 -->
    <el-dialog
      :title="'添加' + titleText"
      :visible.sync="addDialogVisible"
      width="40%"
      @close="addDialogVisibleClose"
    >
      <el-form
        :model="addForm"
        :rules="addRules"
        ref="addForm"
        label-width="90px"
      >
        <el-form-item :label="titleText" prop="attr_name">
          <el-input v-model="addForm.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSure">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改 -->
    <el-dialog
      :title="'修改' + titleText"
      :visible.sync="editDialogVisible"
      width="40%"
    >
      <el-form
        :model="editForm"
        :rules="addRules"
        ref="addForm"
        label-width="90px"
      >
        <el-form-item :label="titleText" prop="attr_name">
          <el-input v-model="editForm.attr_name"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import "@/assets/css/goods/params.scss";

export default {
  data() {
    return {
      cateList: [],
      cateProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      selectedCateKeys: [],
      activeName: "many",
      manyData: [],
      onlyData: [],
      addDialogVisible: false,
      editDialogVisible: false,
      addForm: {
        attr_name: "",
      },
      editForm: {
        attr_name: "",
      },
      addRules: {
        attr_name: [
          { required: true, message: "请输入参数/属性", trigger: "blur" },
        ],
      },
      dynamicTags: [],
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.axios.get("categories");
      if (res.meta.status == 200) {
        this.cateList = res.data;
      } else {
        this.$message.error(res.meta.msg);
      }
    },
    async handleChange() {
      this.getParamsData();
    },
    handleClick() {
      this.getParamsData();
    },
    async getParamsData() {
      if (this.selectedCateKeys.length < 3) {
        this.selectedCateKeys = [];
      } else {
        const { data: res } = await this.axios.get(
          `categories/${this.cateId}/attributes`,
          {
            params: { sel: this.activeName },
          }
        );
        if (res.meta.status == 200) {
          if (this.activeName == "many") {
            this.manyData = res.data;
            res.data.forEach((item) => {
              item.attr_vals = item.attr_vals ? item.attr_vals.split(" ") : [];
              item.inputVisible = false;
              item.inputValue = "";
            });
          } else if (this.activeName == "only") {
            this.onlyData = res.data;
          }
        } else {
          this.$message.error(res.meta.msg);
        }
      }
    },
    addDialogVisibleClose() {
      this.$refs.addForm.resetFields();
    },
    addSure() {
      // this.addDialogVisible = false
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.axios.post(
            `categories/${this.cateId}/attributes`,
            {
              attr_name: this.addForm.attr_name,
              attr_sel: this.activeName,
            }
          );
          if (res.meta.status == 201) {
            this.$message.success(res.meta.msg);
            this.getParamsData();
            this.addDialogVisible = false;
          } else {
            this.$message.error(res.meta.msg);
          }
        } else {
          return false;
        }
      });
    },
    async findId(id) {
      this.editDialogVisible = true;
      const { data: res } = await this.axios.get(
        `categories/${this.cateId}/attributes/${id}`
      );
      if (res.meta.status == 200) {
        this.editForm = res.data;
      } else return;
    },
    editSure() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          const { data: res } = await this.axios.put(
            `categories/${this.cateId}/attributes/${this.editForm.attr_id}`,
            {
              attr_name: this.editForm.attr_name,
              attr_sel: this.activeName,
            }
          );
          if (res.meta.status == 200) {
            this.$message.success(res.meta.msg);
            this.getParamsData();
            this.editDialogVisible = false;
          } else {
            this.$message.error(res.meta.msg);
          }
        } else {
          return false;
        }
      });
    },
    async del(id) {
      const result = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      if (result == "confirm") {
        const { data: res } = await this.axios.delete(
          `categories/${this.cateId}/attributes/${id}`
        );
        if (res.meta.status == 200) {
          this.$message.success(res.meta.msg);
          this.getParamsData();
        } else {
          this.$message.error(res.meta.msg);
        }
      } else return;
    },
    handleClose(index) {
      //   this.dynamicTags.splice(this.dynamicTags.indexOf(index), 1);
    },
    showInput(row) {
      row.inputVisible = true;
      //   this.$nextTick((_) => {
      //     this.$refs.saveTagInput.$refs.input.focus();
      //   });
    },
    handleInputConfirm(row) {
      row.inputVisible = false;

      //   let inputValue = this.inputValue;
      //   if (inputValue) {
      //     this.dynamicTags.push(inputValue);
      //   }
      //   this.inputVisible = false;
      //   this.inputValue = "";
    },
  },
  computed: {
    isDisabled() {
      if (this.selectedCateKeys.length !== 3) {
        return true;
      } else {
        return false;
      }
    },
    cateId() {
      if (this.selectedCateKeys.length == 3) {
        return this.selectedCateKeys[2];
      } else {
        return null;
      }
    },
    titleText() {
      if (this.activeName == "many") {
        return "动态参数";
      } else {
        return "静态属性";
      }
    },
  },
};
</script>

<style scoped></style>
